/*   Shotscreens
 *   Copyright (C) 2010, Vadim Zakondyrin (http://crystalnix.com)
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "paintproperties.h"
#include <QColorDialog>

PaintProperties::PaintProperties(QWidget *parent) :
    QDialog(parent)
{
    ui.setupUi(this);
    scene.setBackgroundBrush(Qt::black);
    ui.testColor->setScene(&scene);

    connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(okAction()));
    connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(cancelAction()));

    connect(ui.colorButton, SIGNAL(clicked()), this, SLOT(colorChanged()));

    connect(ui.fontComboBox, SIGNAL(currentFontChanged(QFont)), this, SLOT(fontChanged(QFont)));
    connect(ui.textSizeBox, SIGNAL(valueChanged(int)), this, SLOT(fontSizeChanged(int)));
    connect(ui.boldBox, SIGNAL(clicked(bool)), this, SLOT(boldChecked(bool)));
    connect(ui.italicBox, SIGNAL(clicked(bool)), this, SLOT(italicChecked(bool)));
    connect(ui.underlineBox, SIGNAL(clicked(bool)), this, SLOT(underlineChecked(bool)));
}

void PaintProperties::okAction()
{
    brushPen.setWidth(ui.sizeBox->value());
    brushPen.setColor(scene.backgroundBrush().color());

    QFont font = ui.fontComboBox->currentFont();
    setTextFontFromInterfaceTo(font);
    setTextFont(font);

    QDialog::hide();
}

void PaintProperties::cancelAction()
{
    QDialog::hide();
}

void PaintProperties::show()
{
    ui.sizeBox->setValue(getBrushPen().width());
    ui.fontComboBox->setCurrentFont(getTextFont());
    ui.testText->setFont(getTextFont());
    ui.textSizeBox->setValue(getTextFont().pointSize());
    ui.boldBox->setChecked(getTextFont().bold());
    ui.italicBox->setChecked(getTextFont().italic());
    ui.underlineBox->setChecked(getTextFont().underline());

    QDialog::show();
}

void PaintProperties::boldChecked(bool checked)
{
    QFont font = ui.testText->font();
    font.setBold(checked);
    ui.testText->setFont(font);
}

void PaintProperties::italicChecked(bool checked)
{
    QFont font = ui.testText->font();
    font.setItalic(checked);
    ui.testText->setFont(font);
}

void PaintProperties::underlineChecked(bool checked)
{
    QFont font = ui.testText->font();
    font.setUnderline(checked);
    ui.testText->setFont(font);
}

void PaintProperties::fontChanged(QFont font)
{
    setTextFontFromInterfaceTo(font);
    ui.testText->setFont(font);
}

void PaintProperties::setTextFontFromInterfaceTo(QFont & toFont)
{
    toFont.setPointSize(ui.textSizeBox->value());
    toFont.setBold(ui.boldBox->isChecked());
    toFont.setItalic(ui.italicBox->isChecked());
    toFont.setUnderline(ui.underlineBox->isChecked());
}

void PaintProperties::fontSizeChanged(int size)
{
    QFont font = ui.testText->font();
    font.setPointSize(size);
    ui.testText->setFont(font);
}

void PaintProperties::colorChanged()
{
    QColor color = QColorDialog::getColor(ui.testColor->backgroundBrush().color());
    scene.setBackgroundBrush(color);
}

void PaintProperties::on_buttonBox_clicked(QAbstractButton* button)
{
    Q_UNUSED(button);
}
